package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YApply;
import com.yqfk.mapper.YApplyMapper;
import com.yqfk.mapper.YLevelMapper;
import com.yqfk.mapper.YUserMapper;
import com.yqfk.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/y-apply")
public class YApplyController extends JSONAuthentication {
    @Autowired
    YApplyMapper yApplyMapper;
    @Autowired
    YLevelMapper yLevelMapper;
    @Autowired
    YUserMapper yUserMapper;
    @ApiOperation(value = "添加外出申请")
    @PostMapping("/add")
    public void addApply(HttpServletRequest request, HttpServletResponse response, @RequestBody YApply yApply) throws Exception {
        if (yLevelMapper.selectById(1).getLevel().equals("高")) {
            yApply.setStatus("未审核");
        }
        if (yApplyMapper.insert(yApply) > 0) {
            Result ok = Result.ok().message("申请成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除外出申请")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = yApplyMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改外出申请")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody YApply yApply) throws Exception {
        if (yApplyMapper.updateById(yApply) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改外出申请状态")
    @PostMapping("/updStatus")
    public void updStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id,@RequestParam("status") String status) throws Exception {
        YApply yApply = yApplyMapper.selectById(id);
        yApply.setStatus(status);
        if (yApplyMapper.updateById(yApply) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部外出申请")
    @PostMapping("/findAllNotice")
    public void findAllNotice(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<YApply> shUserPage = new Page<>(current, size);
        Page<YApply> shUserPages = yApplyMapper.selectPage(shUserPage, null);
        List<YApply> records = shUserPages.getRecords();
        for(YApply a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        int total = yApplyMapper.selectCount(null);
        if (records.size() > 0) {
            Result ok = Result.ok().data("apply", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询未审核的外出申请")
    @PostMapping("/findAllNoticeNoSh")
    public void findAllNoticeNoSh(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","未审核");
        Page<YApply> shUserPage = new Page<>(current, size);
        Page<YApply> shUserPages = yApplyMapper.selectPage(shUserPage, wrapper);
        List<YApply> records = shUserPages.getRecords();
        for(YApply a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        int total = yApplyMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("apply", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询外出申请通过用户id")
    @PostMapping("/findAllNoticeByUserId")
    public void findAllNoticeByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int userId, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<YApply> shUserPage = new Page<>(current, size);
        Page<YApply> shUserPages = yApplyMapper.selectPage(shUserPage, wrapper);
        List<YApply> records = shUserPages.getRecords();
        int total = yApplyMapper.selectCount(wrapper);
        for(YApply a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("apply", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

